<template>
    <div class="default-main">
        <div class="banner">
            <el-row :gutter="10">
                <el-col :md="24" :lg="18">
                    <div class="welcome suspension">
                        <img class="welcome-img" :src="headerSvg" alt="" />
                        <div class="welcome-text">
                            <div class="welcome-title">{{ adminInfo.nickname + t('utils.comma') + getGreet() }}</div>
                        </div>
                    </div>
                </el-col>
                <el-col :lg="6" class="hidden-md-and-down">
                    <div class="working">
                        <img class="working-coffee" :src="coffeeSvg" alt="" />
                        <div class="working-text">
                            {{ t('dashboard.You have worked today') }}<span class="time">{{ state.workingTimeFormat }}</span>
                        </div>
                    </div>
                </el-col>
            </el-row>
        </div>
        <div class="small-panel-box">
            <el-row :gutter="20">
                <el-col :sm="6" :md="6" :lg="3" v-for="(item,index) in state.data.up">
                    <el-badge :value="item.count" style="width:100%">
                        <div class="small-panel user-reg suspension" @click="handleItem(item)">
                            <!-- <Icon :name="'el-icon-'+state.icon[index]" size="42"></Icon> -->
                            <Icon :name="'el-icon-Document'" size="42"></Icon>
                            <h4>{{item.codename}} </h4>
                        </div>
                    </el-badge>
                </el-col>
            </el-row>
        </div>
        <div class="growth-chart">
            <el-row :gutter="20">
                <el-col :xs="24" :sm="24" :md="12" :lg="12" style="margin-bottom: 20px;">
                    <el-card class="new-user-card" shadow="hover" :header="'我的申请'" body-style="padding:10px 20px 20px">
                        <el-table :data="state.data.apply" style="width: 100%">
                            <el-table-column type="index" label="NO" :index="idx=>idx+1" align="center"/>
                            <el-table-column prop="name" label="申请项目" min-width="180" align="center" />
                            <el-table-column prop="approval_d" label="待批数量" align="center" />
                            <el-table-column prop="approval_y" label="已批数量" align="center" />
                            <el-table-column prop="approval_t" label="退回数量" align="center" />
                        </el-table>
                    </el-card>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" style="margin-bottom: 20px;">
                    <el-card class="new-user-card" shadow="hover" :header="'我的审批'" body-style="padding:10px 20px 20px">
                        <el-table :data="state.data.flow" style="width: 100%">
                            <el-table-column type="index" label="NO" :index="idx=>idx+1" align="center"/>
                            <el-table-column prop="name" label="待批项目" min-width="180" align="center" />
                            <el-table-column prop="approval_d" label="当前待批" align="center" />
                            <el-table-column prop="approval_y" label="已审数量" align="center" />
                            <el-table-column prop="approval_t" label="操作" align="center" >
                                <template #default="scope">
                                    <el-button type="primary" link @click="handleApprove(scope.row)">审批</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-card>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" style="margin-bottom: 20px;">
                    <el-card class="new-user-card" shadow="hover" :header="'我的事务'" body-style="padding:10px 20px 20px">
                        <ul class="thing" v-if="state.data.thing && state.data.thing.length">
                            <li v-for="item in state.data.thing">
                            <span class="text" @click="handleThing(item)">{{ item.REMIND }}</span>
                            <span class="status" :class="{red:item.ACTIVED=='0'}">{{ item.ACTIVED=='1'?'已读':'未读' }}</span>
                            </li>
                        </ul>
                        <el-empty v-else  description="暂无数据" />
                    </el-card>
                </el-col>
            </el-row>
        </div>

    </div>
</template>

<script setup lang="ts">
import { onMounted, onUnmounted, reactive, nextTick, onActivated, watch, onBeforeMount } from 'vue'
import headerSvg from '/@/assets/dashboard/header-1.svg'
import coffeeSvg from '/@/assets/dashboard/coffee.svg'
import { CountUp } from 'countup.js'
import * as echarts from 'echarts'
import { useNavTabs } from '/@/stores/navTabs'
import { useTemplateRefsList } from '@vueuse/core'
import { dashboard } from '/@/api/backend/dashboard'
import { useI18n } from 'vue-i18n'
import { Local } from '/@/utils/storage'
import { useAdminInfo } from '/@/stores/adminInfo'
import { WORKING_TIME } from '/@/stores/constant/cacheKey'
import { getGreet } from '/@/utils/common'
import { useEventListener } from '@vueuse/core'
import 'element-plus/theme-chalk/display.css'
var workTimer: NodeJS.Timer

const d = new Date()
const { t } = useI18n()
const navTabs = useNavTabs()
const adminInfo = useAdminInfo()
const chartRefs = useTemplateRefsList<HTMLDivElement>()

const state: {
    workingTimeFormat: string,
    data:anyObj,
    icon:string[]
} = reactive({
    workingTimeFormat: '',
    data:{},
    icon:['Wallet','Document','BellFilled','Checked','']
})

const getIndexData = ()=>{
    createAxios({
        url:'admin/index/index_sy'
    }).then((res:any)=>{
        state.data = res.data
    })
}

const router = useRouter()


const handleItem = (item:any)=>{
    console.log(item)
    let url = ''
    switch (item.codename){
        case '供应商证件逾期':
            url = '/corplib/supplier'
        break;
        case '供应商合同逾期':
            url = '/xContract/supplier'
        break;
        case '供应商安全整改':
            url = '/corplib/supplier'
        break;
        case '罐箱年检':
            url = '/xPotcheck'
        break;
        case '车辆年检':
            url = '/corplib/supplier'
        break;
        case '车辆保险':
            url = '/corplib/supplier'
        break;
        case '驾驶证':
            url = '/corplib/supplier'
        break;
        case '罐箱保险':
            url = '/xInsurance'
        break;
        case '投诉信息':
            url = '/complainthead/customer'
        break;
        case '罐箱分配':
            url = '/xPotfp'
        break;
        case '罐箱申请':
            url = '/xPotsq'
        break;
        case '客户合同':
            url = 'xContract/customer'
        break;
        case '道路运行许可证':
            url = '/corplib/supplier'
        break;
    }
    
    router.push('/admin'+url+'?code='+item.code)
}

const handleApprove = (item:any)=>{
    let url = ''
    switch (item.name){
        case '配件采购':
            url = '/xStockApproval'
        break;
        case '供应商修改':
            url = '/corplib/supplier/approve'
        break;
        case '罐箱维修审批':
            url = '/xRepairrecord/approve'
        break;
        case '报价审核':
            url = '/xBjhead/customer/approve'
        break;
    }

    router.push('/admin'+url)
}

const handleThing = (item:any)=>{
    createAxios({
        url:'/admin/XPersonalremind/read',
        params:{
            id:item.ID
        } 
    })
    router.push('/admin/xPersonalremind')
}

const startWork = () => {
    const workingTime = Local.get(WORKING_TIME) || { date: '', startTime: 0, pauseTime: 0, startPauseTime: 0 }
    const currentDate = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
    const time = parseInt((new Date().getTime() / 1000).toString())

    if (workingTime.date != currentDate) {
        workingTime.date = currentDate
        workingTime.startTime = time
        workingTime.pauseTime = workingTime.startPauseTime = 0
        Local.set(WORKING_TIME, workingTime)
    }

    let startPauseTime = 0
    if (workingTime.startPauseTime <= 0) {
        // state.pauseWork = false
        startPauseTime = 0
    } else {
        // state.pauseWork = true
        startPauseTime = time - workingTime.startPauseTime // 已暂停时间
    }

    let workingSeconds = time - workingTime.startTime - workingTime.pauseTime - startPauseTime

    state.workingTimeFormat = formatSeconds(workingSeconds)
    if (!state.pauseWork) {
        workTimer = setInterval(() => {
            workingSeconds++
            state.workingTimeFormat = formatSeconds(workingSeconds)
        }, 1000)
    }
}

const formatSeconds = (seconds: number) => {
    var secondTime = 0 // 秒
    var minuteTime = 0 // 分
    var hourTime = 0 // 小时
    var dayTime = 0 // 天
    var result = ''

    if (seconds < 60) {
        secondTime = seconds
    } else {
        // 获取分钟，除以60取整数，得到整数分钟
        minuteTime = Math.floor(seconds / 60)
        // 获取秒数，秒数取佘，得到整数秒数
        secondTime = Math.floor(seconds % 60)
        // 如果分钟大于60，将分钟转换成小时
        if (minuteTime >= 60) {
            // 获取小时，获取分钟除以60，得到整数小时
            hourTime = Math.floor(minuteTime / 60)
            // 获取小时后取佘的分，获取分钟除以60取佘的分
            minuteTime = Math.floor(minuteTime % 60)
            if (hourTime >= 24) {
                // 获取天数， 获取小时除以24，得到整数天
                dayTime = Math.floor(hourTime / 24)
                // 获取小时后取余小时，获取分钟除以24取余的分；
                hourTime = Math.floor(hourTime % 24)
            }
        }
    }

    result =
        hourTime +
        t('dashboard.hour') +
        ((minuteTime >= 10 ? minuteTime : '0' + minuteTime) + t('dashboard.minute')) +
        ((secondTime >= 10 ? secondTime : '0' + secondTime) + t('dashboard.second'))
    if (dayTime > 0) {
        result = dayTime + t('dashboard.day') + result
    }
    return result
}


onMounted(() => {
    startWork()
    getIndexData()
})


onUnmounted(() => {
    clearInterval(workTimer)
})

</script>

<script lang="ts">
import { defineComponent } from 'vue'
import createAxios from '/@/utils/axios'
import { useRouter } from 'vue-router'
export default defineComponent({
    name: 'dashboard',
})
</script>

<style scoped lang="scss">
.welcome {
    background: #e1eaf9;
    border-radius: 6px;
    display: flex;
    align-items: center;
    padding: 15px 20px !important;
    box-shadow: 0 0 30px 0 rgba(82, 63, 105, 0.05);
    .welcome-img {
        height: 100px;
        margin-right: 10px;
        user-select: none;
    }
    .welcome-title {
        font-size: 1.5rem;
        line-height: 30px;
        color: var(--ba-color-primary-light);
    }
    .welcome-note {
        padding-top: 6px;
        font-size: 15px;
        color: var(--el-text-color-primary);
    }
}
.working {
    height: 130px;
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    height: 100%;
    position: relative;
    &:hover {
        .working-coffee {
            -webkit-transform: translateY(-4px) scale(1.02);
            -moz-transform: translateY(-4px) scale(1.02);
            -ms-transform: translateY(-4px) scale(1.02);
            -o-transform: translateY(-4px) scale(1.02);
            transform: translateY(-4px) scale(1.02);
            z-index: 999;
        }
    }
    .working-coffee {
        transition: all 0.3s ease;
        width: 80px;
    }
    .working-text {
        display: block;
        width: 100%;
        font-size: 15px;
        text-align: center;
        color: var(--el-text-color-primary);
    }
    .working-opt {
        position: absolute;
        top: -40px;
        right: 10px;
        background-color: rgba($color: #000000, $alpha: 0.3);
        padding: 10px 20px;
        border-radius: 20px;
        color: var(--ba-bg-color-overlay);
        transition: all 0.3s ease;
        cursor: pointer;
        opacity: 0;
        z-index: 999;
        &:active {
            background-color: rgba($color: #000000, $alpha: 0.6);
        }
    }
    &:hover {
        .working-opt {
            opacity: 1;
            top: 0;
        }
        .working-done {
            opacity: 1;
            top: 50px;
        }
    }
}
.small-panel-box {
    margin-top: 20px;
}
.small-panel {
    background-color: #e9edf2;
    border-radius: var(--el-border-radius-base);
    padding: 25px 5px;
    margin-bottom: 20px;
    text-align: center;
    cursor: pointer;
    .el-icon{
        font-size: 36px;
    }
    h4{
        margin-top:20px;
        height: 40px;
    }
}
.growth-chart {
    margin-bottom: 20px;
}
.user-growth-chart,
.file-growth-chart {
    height: 260px;
}
.new-user-growth {
    height: 300px;
}

.user-source-chart,
.user-surname-chart {
    height: 400px;
}
.new-user-item {
    display: flex;
    align-items: center;
    padding: 20px;
    margin: 10px 15px;
    box-shadow: 0 0 30px 0 rgba(82, 63, 105, 0.05);
    background-color: var(--ba-bg-color-overlay);
    .new-user-avatar {
        height: 48px;
        width: 48px;
        border-radius: 50%;
    }
    .new-user-base {
        margin-left: 10px;
        color: #2c3f5d;
        .new-user-name {
            font-size: 15px;
        }
        .new-user-time {
            font-size: 13px;
        }
    }
    .new-user-arrow {
        margin-left: auto;
    }
}
.new-user-card{
    :deep(.el-card__body) {
        padding: 0;
    }
    ul.thing{
        padding: 10px 0 20px;
        li{
            list-style: none;
            display: flex;
            border-bottom: 1px solid var(--el-border-color-extra-light);
            padding: 8px 10px;
            .text{
                flex: 1;
                cursor: pointer;
                &:hover{
                    color: var(--el-color-primary);
                }
            }
            .status{
                padding: 0 10px;
                color: #999;
                &.red{
                    color: var( --el-color-danger);
                }
            }
        }
    }
} 


@media screen and (max-width: 425px) {
    .welcome-img {
        display: none;
    }
}
@media screen and (max-width: 1200px) {
    .lg-mb-20 {
        margin-bottom: 20px;
    }
}
html.dark {
    .welcome {
        background-color: var(--ba-bg-color-overlay);
    }
    .small-panel {
        background-color: var(--ba-bg-color-overlay);
        .small-panel-content {
            color: var(--el-text-color-regular);
        }
    }
    .new-user-item {
        .new-user-base {
            color: var(--el-text-color-regular);
        }
    }
}
</style>
